package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

/**
 * @description:
 * @author: Hupengpeng
 * @time: 2021/7/5 9:26
 */
public class VideoCreate {
    private String open_id;
    private String access_token;
    private String video_id;
    private String text;
    private String poi_id;
    private String poi_name;
    private String micro_app_id;
    private String micro_app_title;
    private String article_id;
    private String article_title;
    private String timeliness_label;
    private String timeliness_keyword;
    private String game_id;
    private String game_content;
    private String cover_tsp;
    private String[] at_users;
    private String micro_app_url;
    private String custom_cover_image_url;
    private String response;

    private VideoCreate() {
    }

    protected static VideoCreate New() {
        return new VideoCreate();
    }

    public VideoCreate execute() {
        String url = StrUtil.format("{}{}?open_id={}&access_token={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_VIDEO_CREATE,
                getOpen_id(),
                getAccess_token()
        );
        HttpRequest rs = HttpUtil.createPost(url).
                form("video_id", getVideo_id()).
                form("text", getText()).
                form("poi_id", getPoi_id()).
                form("poi_name", getPoi_name()).
                form("micro_app_id", getMicro_app_id()).
                form("micro_app_title", getMicro_app_title()).
                form("article_id", getArticle_id()).
                form("article_title", getArticle_title()).
                form("timeliness_label", getTimeliness_label()).
                form("timeliness_keyword", getTimeliness_keyword()).
                form("game_id", getGame_id()).
                form("game_content", getGame_content()).
                form("cover_tsp", getCover_tsp()).
                form("at_users", getAt_users()).
                form("micro_app_url", getMicro_app_url()).
                form("custom_cover_image_url", getCustom_cover_image_url());
        String body = JSONUtil.toJsonStr(rs.form());
        response = rs.body(body).execute().body();
        return this;
    }

    public VideoCreateResponse convert() {
        try {
            JSONObject obj = JSONUtil.parseObj(response);
            JSONObject data = obj.getJSONObject("data");
            if (obj.getJSONObject("extra").getInt("error_code") == 0) {
                return JSONUtil.toBean(data, VideoCreateResponse.class);
            }
        } catch (Exception e) {
            throw new DouYinException(StrUtil.format("{}:{}", e.getMessage(), response));
        }
        throw new DouYinException(response);
    }

    public String getOpen_id() {
        return open_id;
    }

    public VideoCreate setOpen_id(String open_id) {
        this.open_id = open_id;
        return this;
    }

    public String getAccess_token() {
        return access_token;
    }

    public VideoCreate setAccess_token(String access_token) {
        this.access_token = access_token;
        return this;
    }

    public String getVideo_id() {
        return video_id;
    }

    public VideoCreate setVideo_id(String video_id) {
        this.video_id = video_id;
        return this;
    }

    public String getText() {
        return text;
    }

    public VideoCreate setText(String text) {
        this.text = text;
        return this;
    }

    public String getPoi_id() {
        return poi_id;
    }

    public VideoCreate setPoi_id(String poi_id) {
        this.poi_id = poi_id;
        return this;
    }

    public String getPoi_name() {
        return poi_name;
    }

    public VideoCreate setPoi_name(String poi_name) {
        this.poi_name = poi_name;
        return this;
    }

    public String getMicro_app_id() {
        return micro_app_id;
    }

    public VideoCreate setMicro_app_id(String micro_app_id) {
        this.micro_app_id = micro_app_id;
        return this;
    }

    public String getMicro_app_title() {
        return micro_app_title;
    }

    public VideoCreate setMicro_app_title(String micro_app_title) {
        this.micro_app_title = micro_app_title;
        return this;
    }

    public String getArticle_id() {
        return article_id;
    }

    public VideoCreate setArticle_id(String article_id) {
        this.article_id = article_id;
        return this;
    }

    public String getArticle_title() {
        return article_title;
    }

    public VideoCreate setArticle_title(String article_title) {
        this.article_title = article_title;
        return this;
    }

    public String getTimeliness_label() {
        return timeliness_label;
    }

    public VideoCreate setTimeliness_label(String timeliness_label) {
        this.timeliness_label = timeliness_label;
        return this;
    }

    public String getTimeliness_keyword() {
        return timeliness_keyword;
    }

    public VideoCreate setTimeliness_keyword(String timeliness_keyword) {
        this.timeliness_keyword = timeliness_keyword;
        return this;
    }

    public String getGame_id() {
        return game_id;
    }

    public VideoCreate setGame_id(String game_id) {
        this.game_id = game_id;
        return this;
    }

    public String getGame_content() {
        return game_content;
    }

    public VideoCreate setGame_content(String game_content) {
        this.game_content = game_content;
        return this;
    }

    public String getCover_tsp() {
        return cover_tsp;
    }

    public VideoCreate setCover_tsp(String cover_tsp) {
        this.cover_tsp = cover_tsp;
        return this;
    }

    public String[] getAt_users() {
        return at_users;
    }

    public VideoCreate setAt_users(String[] at_users) {
        this.at_users = at_users;
        return this;
    }

    public String getMicro_app_url() {
        return micro_app_url;
    }

    public VideoCreate setMicro_app_url(String micro_app_url) {
        this.micro_app_url = micro_app_url;
        return this;
    }

    public String getCustom_cover_image_url() {
        return custom_cover_image_url;
    }

    public VideoCreate setCustom_cover_image_url(String custom_cover_image_url) {
        this.custom_cover_image_url = custom_cover_image_url;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public VideoCreate setResponse(String response) {
        this.response = response;
        return this;
    }


    public class VideoCreateResponse {
        private String item_id;
        private int error_code;
        private String description;

        public String getItem_id() {
            return item_id;
        }

        public VideoCreateResponse setItem_id(String item_id) {
            this.item_id = item_id;
            return this;
        }

        public int getError_code() {
            return error_code;
        }

        public VideoCreateResponse setError_code(int error_code) {
            this.error_code = error_code;
            return this;
        }

        public String getDescription() {
            return description;
        }

        public VideoCreateResponse setDescription(String description) {
            this.description = description;
            return this;
        }
    }
}
